/**
 * @file 工单操作-一键提醒
 * @author ligang
 * @date 2021-12-17 20:13:16
 */
layui.define(['layer', 'form', 'list_modify_common', '$http'], function (exports) {
    var $ = layui.$;
    let form = layui.form;
    var opCommonUtils = layui.list_modify_common;
    var layer = layui.layer;
    var $http = layui.$http;
    var remindLock = false;

    function getTagShortName(tagId) {
        for (var i = 0; i < tagList.length; i++) {
            var element = tagList[i];
            if (element.value.toString() === tagId) {
                return element.ext1;
            }
        }
    }

    function getTagName(tagId) {
        for (var i = 0; i < tagList.length; i++) {
            var element = tagList[i];
            if (element.value.toString() === tagId) {
                return element.text;
            }
        }
    }

    function openRemindConfirm(data, content, status) {
        if (status === 9011009) {
            layer.open({
                type: 1,
                title: "提示",
                btn: ['我知道了'],
                shade: [0.8, '#393D49'],
                area: ['625px', '200px'],
                scrollbar: false,
                content: '<div style="line-height: 20px; padding: 16px">' + content + '</div>',
                yes: function (index, layero) {
                    layer.close(index); //关闭弹层
                }
            });
        }else {
            layer.open({
                type: 1,
                title: "提示",
                btn: ['已安抚，不再催单', '安抚无效，继续催单'],
                area: ['625px', '200px'],
                shade: [0.8, '#393D49'],
                scrollbar: false,
                content: '<div style="line-height: 20px; padding: 16px">' + content + '</div>',
                yes: function (index, layero) {
                    layer.close(index); //关闭弹层
                }
                , btn2: function (index, layero) {
                    layer.close(index); //关闭弹层
                    saveRemind(data, 2);
                }
            });
        }
    }

    function saveRemind(data, again) {
        let remindParams = {
            orderId: data.field.orderId,
            workId: data.field.workId,
            remark: data.field.remark,
            sceneId:data.field.remindSceneId,
            again: again
        };
        if (!remindLock) {
            if (again == 2) {
                remindLock = true;
            }
            $.ajax({
                url: '/work/modify/save/reminder.action',
                type: "POST",
                async: false, //改为同步请求
                data: JSON.stringify(remindParams),
                contentType: 'application/json;charset=utf-8',
                success: function (res) {
                    switch (res.status) {
                        case 200: {
                            layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                                opCommonUtils.goOperateHome();
                            });
                            return;
                        }
                        case 9011007:{
                            openRemindConfirm(data, res.message, res.status);
                            return;
                        }
                        case 9011008:{
                            openRemindConfirm(data, res.message, res.status);
                            return;
                        }
                        case 9011009:{
                            openRemindConfirm(data, res.message, res.status);
                            return;
                        }
                        default:
                            layer.msg(res.message);
                            return;
                    }
                }
            });
        }
    }

    /**
     * 初始化页面
     */
    function initPage() {
    }


    /**
     * 初始化页面事件
     */
    function initEvent() {

        form.on('radio(type)', function (obj) {
            if (obj.value == 1) {
                $('#remindForm').show();
                $('#contactForm').hide();
                $('#explanation').html("通过机器人自动拨打主工程师电话进行催单！");
            } else {
                $('#remindForm').hide();
                $('#contactForm').show();
                $('#explanation').html("提醒工程师再次联系用户！");
            }
        });

        form.on('submit(save)', function (data) {
            var url = '/work/modify/save/reminder.action';

            let field = data.field;

            if (!field.type) {
                layer.msg("请选择提醒类型！", {"icon": 2})
                return
            }

            // 催单调用催单新接口
            if (field.type == 1) {
                if (!field.remindSceneId) {
                    layer.msg("请选择催单场景！", {"icon": 2})
                    return
                }
                saveRemind(data, 1);
                return false;
            }
            let sceneName = $('input:radio[name="contactSceneId"]:checked').next("div").children("div").text()
            if (!sceneName) {
                layer.msg("请选择联系场景！", {"icon": 2})
                return
            }
            let params = {
                orderId: field.orderId,
                workId: field.workId,
                type: field.type,
                remark: field.remark,
                sceneId: field.contactSceneId,
                sceneName: sceneName
            };
            $http.lockSave(url, params, data.elem).then(function () {
                layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                    opCommonUtils.goOperateHome();
                });
            }, function (msg) {
                layer.msg(msg);
            });

            return false;
        });


    }

    // 程序入口
    function init() {

        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();

    }


    // 初始化
    init();

    exports('oms_work_modify_orderRemind');
});
